<template>
  <div class="app-container">
    <el-tabs v-model="editableTabsValue" type="card" editable @edit="handleTabsEdit">
      <el-tab-pane
        :key="item.name"
        v-for="(item, i) in editableTabs"
        :label="item.title"
        :name="item.name"
      >
        <components :is="components.length + 1 > Number(editableTabsValue) ? components[i] : components[1]"></components>
        <!-- <snowBox v-if="i==0" />
        <loadingBox v-if="i==1" />
        <textBox v-if="i==2" />
        <cardBox v-if="i==3" />
        <burgerMenu v-if="i==4" />
        <buttonBox v-if="i==5" />
        <charts v-if="i==6" /> -->
        <!-- <div v-else>{{item.content}}</div> -->
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  import { getUsername } from '@/utils/auth'
  import Watermark from '@/utils/waterMark'
  import snowBox from './snowBox/snowBox.vue'
  import loadingBox from './loadingBox/loadingBox.vue'
  import textBox from './textBox/textBox.vue'
  import cardBox from './cardBox/cardBox.vue'
  import burgerMenu from './burgerMenu/burgerMenu.vue'
  import buttonBox from './buttonBox/buttonBox.vue'
  import charts from './charts/charts'
  export default {
    components: {
      snowBox,
      loadingBox,
      textBox,
      cardBox,
      burgerMenu,
      buttonBox,
      charts
    },
    data() {
      return {
        // theComponent: 'loadingBox',
        components: [
          'snowBox',
          'loadingBox',
          'textBox',
          'cardBox',
          'burgerMenu',
          'buttonBox',
          'charts'
        ],
        editableTabsValue: '2',
        editableTabs: [
          {
            title: 'Tab 1',
            name: '1',
            content: 'Tab 1 content'
          }, 
          {title: 'Tab 2', name: '2', content: 'Tab 2 content'},
          {title: 'Tab 3', name: '3'},
          {title: 'Tab 4', name: '4'},
          {title: 'Tab 5', name: '5'},
          {title: 'Tab 6', name: '6'},
          {title: 'Tab 7', name: '7'},
          // {title: 'Tab ', name: ''},
        ],
        tabIndex: 2
      }
    },
    mounted() {
    },
    methods: {
      handleTabsEdit(targetName, action) {
        if (action === 'add') {
          this.tabIndex = this.tabIndex < this.editableTabs.length ? this.editableTabs.length : this.tabIndex 
          let newTabName = ++this.tabIndex + '';
          // console.log(newTabName)
          this.editableTabs.push({
            title: 'New Tab',
            name: newTabName,
            // content: 'New Tab content'
          });
          this.editableTabsValue = newTabName;
        }
        if (action === 'remove') {
          let tabs = this.editableTabs;
          let activeName = this.editableTabsValue;
          if (activeName === targetName) {
            tabs.forEach((tab, index) => {
              if (tab.name === targetName) {
                let nextTab = tabs[index + 1] || tabs[index - 1];
                if (nextTab) {
                  activeName = nextTab.name;
                }
              }
            });
          }
          
          this.editableTabsValue = activeName;
          this.editableTabs = tabs.filter(tab => tab.name !== targetName);
        }
      }
    },
    destroyed() {
      // Watermark.set('')
    }
  }
</script>
 
<style scoped lang="scss">
 
</style>